﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;
using System.Collections;

namespace UMC.Wiki.Activities
{
    class SubjectReaderActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Context.Send("Login", true);
            }
            var ProjectId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
           {
               this.Prompt("请输入项目");

               return this.DialogValue("Project");
           })) ?? 0;
            var project = DataFactory.Instance().Project(ProjectId);
            if (project.ReaderPrice > 0)
            {
                var model = this.AsyncDialog("Model", r =>
                {
                    var webr = UMC.Web.WebResource.Instance();
                    var logoUrl = webr.ImageResolve(request.Url, project.Id.Value, "1", 4);
                    var Discount = new UIHeader.Portrait(logoUrl);
                    Discount.Value(project.Caption);
                    Discount.Time(project.Description);

                    var color = 0x63b359;
                    Discount.Gradient(color, color);
                    var header = new UIHeader();
                    var title = UITitle.Create();

                    title.Title = "项目订阅";
                    title.Style.BgColor(color);
                    title.Style.Color(0xfff);

                    header.AddPortrait(Discount, new UIStyle().Name("image-radius", "10"));

                    var ui = UISection.Create(header, title);

                    UIIconNameDesc uIIcon = new UIIconNameDesc();//
                    uIIcon.Put("icon", '\uF0c0').Put("color", "#40c9c6").Put("name", "团队规模").Put("desc", project.MemberTotal + "人");

                    ui.Add(uIIcon);

                    uIIcon = new UIIconNameDesc(new UIIconNameDesc.Item('\uF02d', "文章数量", project.SubjectTotal + "篇").Color(0x36a3f7), new UIIconNameDesc.Item('\uf06e', "浏览总数", project.ViewTotal + "次").Color(0x34bfa3));
                    ui.Add(uIIcon);

                    var price = new UIProductPrice(new UMC.Web.WebMeta().Put("title", "订阅价").Put("price", $"{project.ReaderPrice:0.00}").Put("unit", "期").Put("value", $"{project.TrialDays ?? 7}天之内"));

                    price.Price("只需{2:def}¥{1:price}元，永久订阅{6:def}");

                    price.Caption($"可退款");
                    price.Style.Name("value", new UIStyle().Color(0xdb3652)).Name("def", new UIStyle().Size(13).Color(0x999)).Name("price", new UIStyle().Size(20).Color(0xdb3652));
                    price.Tag("热卖");

                    ui.NewSection().Add(price);
                    var ui2 = ui.NewSection();

                    // UITitleMore more = new UITitleMore("订阅介绍");
                    // more.Style.BorderAll();
                    // ui2.Add(more);
                    var context = DataFactory.Instance().Content(project.Id.Value)?.Content;
                    if (String.IsNullOrEmpty(context))
                    {
                        ui2.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有订阅介绍说明").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                       new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                    }
                    else
                    {
                        ui2.DisableSeparatorLine();
                        ui2.AddCells(Data.Markdown.Transform(context));
                    }

                    ui.UIFootBar = new UIFootBar() { IsFixed = true };

                    ui.UIFootBar.AddText(new UIEventText("联系客服").Click(UIClick.Tel(project.Tel)),
                                     new UIEventText("立即订阅").Click(new UIClick(request, r, "Pay")).Style(new UIStyle().BgColor()));

                    ui.SendTo($"{request.Model}.{request.Command}", this.Context);


                });
                if (DataFactory.Instance().ProjectReader(user.Id.Value, project.Id.Value) != null)
                {
                    this.Prompt("此项目已订阅");
                }
                switch (model)
                {
                    case "Pay":
                        var orderId = this.AsyncDialog("Order", "System", "Payment", new WebMeta("Subject", $"订阅{project.Caption}", "Order", $"{Utility.Guid(user.Id.Value)}-{project.Id.Value}").Put("Price", project.ReaderPrice));

                        goto default;
                    default:
                        var OrderStatus = this.AsyncDialog("OrderStatus", "System", "Paymented", new WebMeta("Order", $"{Utility.Guid(user.Id.Value)}-{project.Id.Value}"), true);
                        var price = Utility.IntParse(this.AsyncDialog("OrderStatus_Text", "0"), 0);

                        if (OrderStatus == "success")
                        {
                            DataFactory.Instance().Put(new Project { Id = project.Id.Value, ReaderTotal = (project.ReaderTotal ?? 0) + 1 });

                            DataFactory.Instance().Put(new ProjectReader
                            {
                                project_id = project.Id.Value,
                                reader_id = user.Id.Value,
                                IsUser = true,
                                JoinTime = Utility.TimeSpan(),
                                Alias = user.Alias,
                                Price = price
                            });
                            this.Context.Send(true);
                        }
                        else
                        {
                            this.Prompt("未订阅成功");
                        }
                        break;
                }

            }
            else
            {
                this.Context.Send(true);

            }
        }

    }
}